use std::fs;

use tokio_cron_scheduler::{Job, JobScheduler};
use chrono::FixedOffset;
use crate::utils::image_dir;
pub async fn init_cron() {
    let scheduler = JobScheduler::new().await.expect("##init cron err");
    let china_timezone = FixedOffset::east_opt(8 * 3600).unwrap();
    let job = Job::new_cron_job_async_tz("0 0 0 * * *",china_timezone, |_uuid, _lock| {
        Box::pin(async move {
            clear_temp_images().await;
        })
    })
    .unwrap();
    let _ = scheduler.add(job).await;

    let _ = scheduler.start().await.expect("##star scheduler err");
}
//清理上传的临时图片
pub async fn clear_temp_images() {
    let mut image_dir = image_dir();
    image_dir.push_str("/temp/");
    fs::read_dir(image_dir).unwrap().into_iter().for_each(|f| {
        fs::remove_file(&f.unwrap().path()).unwrap_or_else(|e| {
            log::error!("##remove temp images err:{}", e);
        });
    });
}
